using System.Windows.Forms;
using ScottIsAFool;
using WindowsLive.Writer.Api;

namespace ProfanityChecker
{
    [WriterPlugin("f92fd820-d3cd-46b1-8b70-dccbd76adb11", "Profanity Checker",
        PublisherUrl = "http://scottisafooldev.spaces.live.com",
        Description = " ",
        ImagePath = "writer.png",
        HasEditableOptions = true)]

    public class Plugin : PublishNotificationHook
    {
        Settings m_default;
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_default = new Settings(pluginOptions);
        }

        public override bool OnPrePublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            return CheckForBannedWords(publishingContext.PostInfo);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            frmOptions op = new frmOptions(m_default);
            //frmPassword op = new frmPassword(m_default);
            if (op.Login() == DialogResult.OK)
            {
                op.ShowDialog();
            }
        }

        private bool CheckForBannedWords(IPostInfo iPostInfo)
        {
            foreach (string s in WriterUtils.ParseString(m_default.BannedWords))
            {
                if (iPostInfo.Contents.ToLower().Contains(s.ToLower()) ||
                    iPostInfo.Title.ToLower().Contains(s.ToLower()))
                {
                    return false;
                }
            }
            return true;
        }

    }
}
